﻿using HandyControl.Themes;
using SuporeSliplampApp.Base;
using SuporeSliplampApp.Service;
using SuporeSliplampApp.Util;
using SuporeSliplampApp.ViewModels;
using SuporeSliplampApp.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SuporeSliplampApp
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CN");
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
            //ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light;
            //if (SerialPortHelper.ConnectPort())
            //{
            //    LoginViewModel.GetInstance().ServerIcon = "\xe6b9";
            //    LoginViewModel.GetInstance().IconColor = "Green";
            //}
            base.OnStartup(e);

            if (File.Exists("DateSettings.json"))
            {
                string json = File.ReadAllText("DateSettings.json");
                var settings = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
                Common.DateIndex = settings.DateCode;
            }

            if (File.Exists("LanguageSettings.json"))
            {
                string json = File.ReadAllText("LanguageSettings.json");
                var settings = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
                if (settings != null && settings.LanguageCode != null)
                {
                    string languageCode = settings.LanguageCode;
                    string uristring = $"pack://application:,,,/Resource/{languageCode}.xaml";
                    Application.Current.Resources.MergedDictionaries[3].Source = new Uri(uristring);
                    Common.languageCode = languageCode;
                }
            }

            if (new LoginWindow().ShowDialog() == true)
            {
                // new MainWindow().ShowDialog();
                new HomeView().ShowDialog();
                //new VideoView().ShowDialog();
            }
            if (Application.Current != null)
            {
                Application.Current.Shutdown();
            }
        }

        protected override void OnExit(ExitEventArgs e)
        {
            //SocketService.StopService();
            base.OnExit(e);
        }

    }
}
